{% extends "base.html" %}

{% block title %}照护计划总表 - 长者照护计划系统{% endblock %}

{% block page_title %}照护计划总表{% endblock %}

{% block extra_css %}
<style>
    .filter-section {
        margin-bottom: 20px;
        padding: 15px;
        border: 1px solid #dee2e6;
        border-radius: 5px;
        background-color: #f8f9fa;
    }
    
    .schedule-table {
        font-size: 0.9rem;
    }
    
    .schedule-table th, .schedule-table td {
        padding: 0.5rem;
    }
    
    .schedule-row:nth-child(odd) {
        background-color: rgba(0, 0, 0, 0.05);
    }
    
    .schedule-row:hover {
        background-color: rgba(0, 123, 255, 0.1);
    }
    
    .json-display {
        background-color: #f8f9fa;
        border: 1px solid #dee2e6;
        border-radius: 4px;
        padding: 10px;
        font-family: monospace;
        white-space: pre-wrap;
        max-height: 300px;
        overflow-y: auto;
    }
    
    #scheduleDataModal .modal-dialog {
        max-width: 90%;
    }
</style>
{% endblock %}

{% block content %}
<div class="row">
    <div class="col-md-12">
        <div class="card">
            <div class="card-header">
                <h5 class="card-title">照护计划总表</h5>
            </div>
            <div class="card-body">
                <div class="filter-section">
                    <form id="scheduleFilterForm">
                        <div class="row">
                            <div class="col-md-4">
                                <label for="elder" class="form-label">老人</label>
                                <input type="text" class="form-control" id="elder" name="elder" placeholder="输入老人ID">
                            </div>
                            <div class="col-md-4">
                                <label for="date" class="form-label">日期</label>
                                <input type="date" class="form-control" id="date" name="date" value="{{ date.today().isoformat() }}">
                            </div>
                            <div class="col-md-4">
                                <label class="form-label">&nbsp;</label>
                                <div class="d-grid">
                                    <button type="button" id="filterButton" class="btn btn-primary">筛选</button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
                
                <div class="table-responsive">
                    <table class="table table-bordered schedule-table">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>老人</th>
                                <th>日期</th>
                                <th>批次号</th>
                                <th>时间段数量</th>
                                <th>创建时间</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody id="scheduleTableBody">
                            {% for result in results %}
                            <tr class="schedule-row" data-elder="{{ result.elder }}" data-date="{{ result.schedule_date }}">
                                <td>{{ result.id }}</td>
                                <td>{{ result.elder }}</td>
                                <td>{{ result.schedule_date }}</td>
                                <td>{{ result.batch_number if result.batch_number else '无' }}</td>
                                <td>{{ result.slots_count if result.slots_count else '0' }}</td>
                                <td>{{ result.create_time if result.create_time else result.schedule_date }}</td>
                                <td>
                                    <button class="btn btn-sm btn-info view-schedule-btn" data-id="{{ result.id }}" data-elder="{{ result.elder }}" data-date="{{ result.schedule_date }}">查看</button>
                                    <button class="btn btn-sm btn-secondary view-data-btn" data-id="{{ result.id }}" data-schedule-data="{{ result.schedule_data }}">数据</button>
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 照护计划数据模态框 -->
<div class="modal fade" id="scheduleDataModal" tabindex="-1" aria-labelledby="scheduleDataModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="scheduleDataModalLabel">照护计划数据</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div id="scheduleDataContent" class="json-display"></div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
    $(document).ready(function() {
        // 筛选按钮点击事件
        $('#filterButton').click(function() {
            const elder = $('#elder').val().toLowerCase();
            const date = $('#date').val();
            
            // 筛选表格行
            $('.schedule-row').each(function() {
                const rowElder = $(this).data('elder').toString().toLowerCase();
                const rowDate = $(this).data('date');
                
                const elderMatch = !elder || rowElder.includes(elder);
                const dateMatch = !date || rowDate.includes(date);
                
                if (elderMatch && dateMatch) {
                    $(this).show();
                } else {
                    $(this).hide();
                }
            });
        });
        
        // 查看排班按钮点击事件
        $(document).on('click', '.view-schedule-btn', function() {
            const id = $(this).data('id');
            const elder = $(this).data('elder');
            const date = $(this).data('date');
            
            // 跳转到查看排班页面
            window.open(`/page/view_schedule_page?elder=${elder}&date=${date}`, '_blank');
        });
        
        // 查看数据按钮点击事件
        $(document).on('click', '.view-data-btn', function() {
            const id = $(this).data('id');
            const scheduleData = $(this).data('schedule-data');
            
            // 显示数据
            try {
                let formattedData = scheduleData;
                if (typeof scheduleData === 'string') {
                    formattedData = JSON.parse(scheduleData);
                }
                $('#scheduleDataContent').text(JSON.stringify(formattedData, null, 2));
            } catch (e) {
                $('#scheduleDataContent').text(scheduleData);
            }
            
            // 显示模态框
            const modal = new bootstrap.Modal(document.getElementById('scheduleDataModal'));
            modal.show();
        });
        
        // 格式化JSON显示
        function formatJSON(json) {
            if (typeof json === 'string') {
                try {
                    json = JSON.parse(json);
                } catch (e) {
                    return json;
                }
            }
            return JSON.stringify(json, null, 2);
        }
    });
</script>
{% endblock %}
